<template>
	<view>
		<view class="margin-bottom-sm" v-for="(item,index) in userAddress" :key="index">
			<view class="padding-lr padding-tb-sm solid-bottom bg-white" @tap="toAddressView(item.id)">
				<view class="flex align-center">
					<view class="cu-avatar lg round">
						<view class="padding">{{item.real_name.substring(0,1)}}</view>
					</view>
					<view class="padding-left response">
						<view class="flex justify-between padding-bottom-sm">
							<view class="">{{item.real_name}}</view>
							<view class="">18269179369</view>
						</view>
						<view class="">
							{{item.province}}{{item.city}}{{item.district}}{{item.detail}}
						</view>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white">
				<view class="action padding-left" @tap="setDefaultAddress(item.id, index)"><text class="cuIcon-roundcheckfill" :class="item.is_default ? 'text-red' : ''"></text>默认地址</view>
				<view class="action">
					<view class="padding-right-sm" @tap="toAddressView(item.id)"><text class="cuIcon-edit text-grey"></text>编辑</view>
					<view @tap="deleteUserAddress(item.id, index)"><text class="cuIcon-delete text-grey"></text>删除</view>
				</view>
			</view>
		</view>
		<view class="cu-tabbar-height"></view>
		
		<view class="cu-bar tabbar border foot bg-white">
			<view class="cu-bar btn-group">
				<button class="cu-btn round shadow-blur bg-red" @tap="toAddressView(false)"><view class="cuIcon-locationfill padding-right-xs"></view>新增地址</button>
				<button class="cu-btn round shadow-blur bg-green text-white" @tap="importAddress"><view class="cuIcon-weixin padding-right-xs"></view>导入微信地址</button>
			</view>
		</view>
		
		<zhi-authorize></zhi-authorize>
	</view>
</template>
<script>
	const app = getApp()
	import user from '@/common/user.js'
	import util from '@/common/util.js'
	import zhiAuthorize from '@/components/zhi-authorize/zhi-authorize.vue'
	export default {
		components: {
			zhiAuthorize
		},
		data() {
			return {
				userAddress: [],
			}
		},
		onLoad() {
			uni.$on('listenAuthorize', (e) => {
				this.getUserAddress()
			})
		},
		onShow() {
			if (app.globalData.isLogin) {
				this.getUserAddress()
			}
		},
		methods: {
			/* 获取用户所有地址 */
			async getUserAddress() {
				try{
					let res = await this.$api.userAddressIndex()
					this.userAddress = res.data.data
				}catch(e){
					util.Toast({ title: e.msg })
				}
				uni.$off('listenAuthorize')
			},
			/* 设置默认地址 */
			async setDefaultAddress(id, index) {
				let that = this
				if (!id) util.Toast({ title: '您设置的默认地址不存在！' })
				try{
					let res = await this.$api.userAddressDefault({id})
					util.Toast({ title: res.msg, icon: 'success' }, function() {
						for (let i = 0, len = that.userAddress.length; i< len; i++) {
							if (i == index) that.userAddress[i].is_default = true
							else that.userAddress[i].is_default = false
						}
					})
				}catch(e){
					util.Toast({ title: e.msg })
				}
			},
			/* 删除地址 */
			async deleteUserAddress(id, index) {
				let that = this
				try{
					let res = await this.$api.userAddressDelete({ id })
					util.Toast({ title: res.msg, mask: true, duration: 1000 }, function() {
						that.userAddress.splice(index, 1)
					})
				}catch(e){
					util.Toast({ title: e.msg })
				}
			},
			/* 新增或编辑 */
			toAddressView(id) {
				if (id) {
					uni.navigateTo({ url: '/pages/user/address/edit?id=' + id })
				} else {
					uni.navigateTo({ url: '/pages/user/address/edit' })
				}
			},
			/* 导入微信地址 */
			importAddress() {
				let that = this
				user.importWechatAddress().then(res => {
					that.$api.userAddressEdit(res).then(res => {
						util.Toast({ title: '导入成功', icon: 'success' })
					}).catch(err => {
						util.Toast({ title: err.msg })
					})
				})
			}
		}
	}
</script>

<style lang="scss"></style>
